package gwtappcontainer.testhelpers;

import static org.junit.Assert.assertTrue;
import gwtappcontainer.server.apis.security.SecurityAPI;
import gwtappcontainer.shared.apis.APIResponse;
import gwtappcontainer.shared.apis.security.UserProp;

import java.io.File;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;


public class APITestHelperTest3 {
	private final APITestHelper3 helper = new APITestHelper3();
	private final UserService userService = UserServiceFactory.getUserService();
	
	@Before
	public void setUp() {
		helper.setUp();
	}
	
	@After 
	public void tearDown() {
		helper.tearDown();
	}
	
	@Test
	public void loginAsInvalidUserTest() {		
		helper.loginAsInvalidUser();
		
		User user = userService.getCurrentUser();
		assertTrue(user != null);
		assertTrue(user.getEmail().equals(helper.getEmail()));	
		
		SecurityAPI api = new SecurityAPI();
		APIResponse response = api.getUser(helper.getEmail());
		assertTrue(response.object == null);				
	}
	
	@Test
	public void loginAsValidUserTest() {		
		helper.loginAsValidUser();
		
		User user = userService.getCurrentUser();
		assertTrue(user != null);
		assertTrue(user.getEmail().equals(helper.getEmail()));		
		
		SecurityAPI api = new SecurityAPI();
		APIResponse response = api.getUser(helper.getEmail());
		assertTrue(response.object != null);
	}
	
	@Test
	public void loginWithPrivilegeTest() {		
		helper.loginWithPrivilege("test");
		
		User user = userService.getCurrentUser();
		assertTrue(user != null);
		assertTrue(user.getEmail().equals(helper.getEmail()));		
		
		SecurityAPI api = new SecurityAPI();
		APIResponse response = api.getUser(helper.getEmail());
		UserProp prop = (UserProp) response.object;
		assertTrue(prop.email.equals(helper.getEmail()));
		
		assertTrue(prop.privileges.contains("TEST"));
		
		//add another privilege. 
		helper.loginWithPrivilege("test2");
		//old privilage should be removed
		response = api.getUser(helper.getEmail());
		prop = (UserProp) response.object;	
		assertTrue(prop.privileges.contains("TEST2"));
		assertTrue(! prop.privileges.contains("TEST"));
		
	}
	
	@Test
	public void loginAsSuperUserTest() {

		helper.loginAsSuperUser();
		
		User user = userService.getCurrentUser();
		assertTrue(user != null);
		assertTrue(user.getEmail().equals("sathyanarayanant@gmail.com"));				
	}	
	
	@Test
	public void deleteTempFiles() {
		String workingDir = System.getProperty("user.dir");
		System.out.println("Working Directory = " +
	              System.getProperty("user.dir"));
		
		String dataDir = workingDir + "\\WEB-INF\\appengine-generated\\";
		String dbBinFile = dataDir + "local_db.bin";
		String indexesFile = dataDir + "datastore-indexes-auto.xml";
		
		try {
			File file = new File(dbBinFile);
			file.delete();
			file = new File(indexesFile);
			file.delete();
			assertTrue(true);
		
		} catch (Exception ex) {
			System.out.println("exception when deleting temp files: " 
				+ ex.getMessage());			
		}
	}
}
